//
//  SCDBUser.swift
//  
//
//  Created by TimTiger on 16/7/16.
//
//

import Foundation
import CoreData


class SCDBUser: NSManagedObject {

    //MARK: - Public API
    /** 更新数据库里用户的信息 */
    class func updateUserInfo(userInfo: SCUserInfo) {
        var dbUser = SCDBUser.dbUserWithUserID(userInfo.userID)
        if dbUser == nil {
            dbUser = NSEntityDescription.insertNewObjectForEntityForName("SCDBUser", inManagedObjectContext: SCDBController.shareController.managedObjectContext) as? SCDBUser
        }
        dbUser?.updateWithUserInfo(userInfo)
        SCDBController.shareController.saveContext()
    }
    
    /** 查询UserInfo */
    class func userInfoWithUserID(userID: String)->SCUserInfo? {
        let dbUser = SCDBUser.dbUserWithUserID(userID)
        if dbUser != nil {
            return dbUser?.converToUserInfo()
        }
        return nil
    }
    
    //MARK: - Private API
    private class func dbUserWithUserID(userID: String)->SCDBUser? {
        let fetchRequest = NSFetchRequest()
        let entityDescription = NSEntityDescription.entityForName("SCDBUser", inManagedObjectContext: SCDBController.shareController.managedObjectContext)
        fetchRequest.entity = entityDescription
        let predicate: NSPredicate = NSPredicate(format: "userID == %@",userID)
        fetchRequest.predicate = predicate
        let sortDescriptor = NSSortDescriptor(key: "userID",ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        fetchRequest.returnsObjectsAsFaults = false
        let resultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: SCDBController.shareController.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        var success: Bool
        do {
            try resultsController.performFetch()
            success = true
        } catch _ as NSError {
            success = false
        }
        if success == true {
            if resultsController.fetchedObjects != nil && resultsController.fetchedObjects!.count > 0 {
                return resultsController.fetchedObjects!.first as? SCDBUser
            }
        }
        return nil
    }
    
    
    /** 转换成UserInfo类 */
    private func converToUserInfo()->SCUserInfo {
        let userInfo = SCUserInfo()
        if String.hasCharacter(self.userID) {
            userInfo.userID = self.userID!
        }
        if String.hasCharacter(self.nickname) {
            userInfo.nickname = self.nickname!
        }
        if String.hasCharacter(self.pushToken) {
            userInfo.pushToken = self.pushToken!
        }
        if String.hasCharacter(self.avatar) {
            userInfo.avatar = self.avatar!
        }
        if self.relationship != nil {
            userInfo.relationship = SCUserRelationship.relationshipWithRawValue(self.relationship!.integerValue)
        }
        return userInfo
    }
    
   private func updateWithUserInfo(userInfo: SCUserInfo) {
        self.avatar = userInfo.avatar
        self.nickname = userInfo.nickname
        self.pushToken = userInfo.pushToken
        self.userID = userInfo.userID
        self.relationship = NSNumber(integer: userInfo.relationship.rawValue)
    }
    
}
